# coding: UTF-8
# Filename : Mysql.py
# Company : imedia
# Author : rose,administrator
# -*-coding:utf-8 -*-

#读取配置文件
import ConfigParser
import hashlib,time
file_path = '/mnt/hgfs/log_to_db/init.ini';
def readFromIni(key):
    config = ConfigParser.ConfigParser()
    value = ''
    with open(file_path, 'r') as cfgfile:
        config.readfp(cfgfile)
        value = config.get('info', key)
        return value
#获取字符串MD5值
def getMD5(str):
    if str == None or str.strip() == '' : return ''
    else : return hashlib.md5(str).hexdigest()
import re
#验证是否为数字
def is_num(str):
    return regex('^\d+$',str)
#做正则验证
def regex(regex, str):
    pattern = re.compile(regex)  
    match = pattern.match(str.strip())  
    if match:  
        return True
    else:
        return False

#过滤html标签    
def filterHtmlTag(str):
    if str == None or str == '': return '' 
    regex = '<.*?>'
    regex_nr = '\s*'
    str =  re.sub(regex,'', str)
    str =  re.sub(regex_nr,'', str)
    return str

#截取字符串长度
def sub_str(n, str):
    if str == None or str == '': return ''
    str = str.decode()
    if n == len(str.strip()): return str
    str = str[0:n]
    return str

#获取列表页面详细信息
def get_detail_text(str):
    str = filterHtmlTag(str)
    return sub_str(30, str)

#分页算法
def get_star_row(size, pagenum):
    if size == '' or pagenum == '': return 0
    if size == 0 or pagenum == 0: return 0
    s = int(size)
    p = int(pagenum)
    return s*(p -1)
      
#计算当前日期是  当年的第多少个星期
def returnWeekNum(date):
    date_split = date.split('-')
    year = int(date_split[0])
    month = int(date_split[1])
    day = int(date_split[2])
    d1=datetime.date(year,month,day)
    dds = d1.isocalendar()
    return dds
import datetime, calendar   
  
def getYesterday():   
    today=datetime.date.today()   
    oneday=datetime.timedelta(days=1)   
    yesterday=today-oneday   
    return yesterday   
      
def getToday():   
    return datetime.date.today()     
  
#获取给定参数的前几天的日期，返回一个list   
def getDaysByNum(num):   
     today=datetime.date.today()   
     oneday=datetime.timedelta(days=1)       
     li=[]        
     for i in range(0,num):   
        #今天减一天，一天一天减   
         today=today-oneday   
        #把日期转换成字符串   
        #result=datetostr(today)   
         li.append(datetostr(today))   
     return li   
  
#将字符串转换成datetime类型   
def strtodatetime(datestr,format):       
    return datetime.datetime.strptime(datestr,format)   
  
#时间转换成字符串,格式为2008-08-02   
def datetostr(date):     
    return    str(date)[0:10]   
  
#两个日期相隔多少天，例：2008-10-03和2008-10-01是相隔两天   
def datediff(beginDate,endDate):   
     format="%Y-%m-%d";   
     bd=strtodatetime(beginDate,format)   
     ed=strtodatetime(endDate,format)       
     oneday=datetime.timedelta(days=1)   
     count=0  
     while bd!=ed:   
         ed=ed-oneday   
         count+=1  
     return count   
  
#获取两个时间段的所有时间,返回list   
def getDays(beginDate,endDate):   
     format="%Y-%m-%d";   
     bd=strtodatetime(beginDate,format)   
     ed=strtodatetime(endDate,format)   
     oneday=datetime.timedelta(days=1)   
     num=datediff(beginDate,endDate)+1   
     li=[]   
     for i in range(0,num):   
         li.append(datetostr(ed))   
         ed=ed-oneday   
     return li   
  
#获取当前年份 是一个字符串   
def getYear():   
    return str(datetime.date.today())[0:4]   
  
#获取当前月份 是一个字符串   
def getMonth():   
    return str(datetime.date.today())[5:7]   
  
#获取当前天 是一个字符串   
def getDay():   
    return str(datetime.date.today())[8:10]      
def getNow():   
    return datetime.datetime.now()
#获取当前时间戳
def getTimestamp():
    return int(time.time())

#字符串转换时间戳
def strToTimestamp(timestr, format):
    obj = strtodatetime(timestr, format)
    return int(time.mktime(obj.timetuple()))   

import random
#随即生成n位密钥
def get_random_str(n = 6):
    num_arrays = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
    rst = ''
    for i in range(0, n):
        num = random.randint(0,9)
        rst += str(num_arrays[num])
    return rst